
package com.xiaoma.myaudience.biz.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.adapter.ActivitySelectAdapter;
import com.xiaoma.myaudience.biz.adapter.HeadlineAdapter;
import com.xiaoma.myaudience.biz.adapter.HomeHotListAdapter;
import com.xiaoma.myaudience.biz.adapter.HomeListAdapter;
import com.xiaoma.myaudience.biz.model.ActivityDetailInfoModel;
import com.xiaoma.myaudience.biz.model.ActivityInfoModel;
import com.xiaoma.myaudience.biz.model.ActivitySelectInfoModel;
import com.xiaoma.myaudience.biz.task.ActivitySelectTask;
import com.xiaoma.myaudience.biz.task.ActivitysTask;
import com.xiaoma.myaudience.biz.task.DataAsyncTask;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.biz.view.HeadlineGallery;
import com.xiaoma.myaudience.biz.view.HorizontalListView;
import com.xiaoma.myaudience.util.Pair;
import com.xiaoma.myaudience.util.PrefHelper;
import com.xiaoma.myaudience.util.activity.BaseActivity;
import com.xiaoma.myaudience.util.model.ModelUtils;

/**
 * 活动页面.
 * 
 */
public class ActivitysActivity extends BaseActivity implements  OnItemClickListener, OnClickListener {
    
    private static final String TAG = "AcivitysActivity";
    private static final int DIALOG_REFRESH = 0;
    private static final int MENU_REFRESH = Menu.FIRST;
    
    public static final String ACTIVITY_CATEGORY = "activity_category";
    public static final String ACTIVITY_CATEGORY_INDEX = "index";
    public static final String ACTIVITY_CATEGORY_INDEX_Y = "index_y";
    public static final String[] ACTIVITY_CATEGORY_TITLE = {"", "", "", "", "", ""};
    private HeadlineGallery mHeadlineGallery;
    private ActivitysTask mActivitysTask;
    private Map<String,String> cateMap = new HashMap<String, String>();
    List<TextView> textViews = new ArrayList<TextView>();
    List<HorizontalListView> listViews = new ArrayList<HorizontalListView>();
	private HashMap<String, String> mapcookie;
	private ActivitySelectTask mActivitySelectTask;
	private HashMap<String, List<Map<String, Object>>> allDataListMap;
	private HashMap<String, HomeListAdapter> allListAdapterMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_layout);
        
        setNetEaseTitle(R.string.category_tab);
        setNetEaseTitleGravity(Gravity.CENTER);
        setNetEaseTitleBarVisibility(View.VISIBLE);
        setNetEaseTitleIcon(R.drawable.home_title_icon);
        initView();
    }
    
    @Override
    protected void onRestart() {
    	// TODO Auto-generated method stub
    	super.onRestart();
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	execute();
    }
    private void initView() {
    	TextView oneTextView = (TextView) findViewById(R.id.one);
    	TextView twoTextView = (TextView) findViewById(R.id.two);
    	TextView threeTextView = (TextView) findViewById(R.id.three);
    	TextView fourTextView = (TextView) findViewById(R.id.four);
    	TextView fiveTextView = (TextView) findViewById(R.id.five);
    	TextView sixTextView = (TextView) findViewById(R.id.six);
    	
    	oneTextView.setOnClickListener(this);
    	twoTextView.setOnClickListener(this);
    	threeTextView.setOnClickListener(this);
    	fourTextView.setOnClickListener(this);
    	fiveTextView.setOnClickListener(this);
    	sixTextView.setOnClickListener(this);
    	
    	textViews.add(oneTextView);
    	textViews.add(twoTextView);
    	textViews.add(threeTextView);
    	textViews.add(fourTextView);
    	textViews.add(fiveTextView);
    	textViews.add(sixTextView);
    	
    	HorizontalListView one = (HorizontalListView) findViewById(R.id.one_list);
    	HorizontalListView two = (HorizontalListView) findViewById(R.id.two_list);
    	HorizontalListView three = (HorizontalListView) findViewById(R.id.three_list);
    	HorizontalListView four = (HorizontalListView) findViewById(R.id.four_list);
    	HorizontalListView five = (HorizontalListView) findViewById(R.id.five_list);
    	HorizontalListView six = (HorizontalListView) findViewById(R.id.six_list);
    	
    	listViews.add(one);
    	listViews.add(two);
    	listViews.add(three);
    	listViews.add(four);
    	listViews.add(five);
    	listViews.add(six);
    }
    
    private void execute() {
    	 cancel();
    	 mActivitysTask = new ActivitysTask(this, DataAsyncTask.TASK_ACTIVITYS_INFO, this);
         mActivitysTask.execute();
	}
    
    private void cancel() {
    	if (mActivitysTask != null) {
    		mActivitysTask.cancel(true, true);
        }
    	mActivitysTask = null;
    }

    private void initData(List<Map<String, Object>> objMap) {
    	
    	allDataListMap = new HashMap<String, List<Map<String, Object>>>();
    	allListAdapterMap = new HashMap<String, HomeListAdapter>();
    	int i = 0;
    	if (objMap != null) {
			for (Map<String, Object> map : objMap) {
		        String className = ModelUtils.getStringValue(map, ActivityInfoModel.PARAM_CLASSNAME);
		        String classid = ModelUtils.getStringValue(map, "classid");
		        List<Map<String, Object>> classEvents = ModelUtils.getListMapValue(map, ActivityInfoModel.PARAM_CLASS_EVENTS); 
		        allDataListMap.put(classid, classEvents);
		        
		        HomeListAdapter adapter = new HomeListAdapter(this, classEvents,classid);
		        allListAdapterMap.put(classid, adapter);
		        listViews.get(i).setAdapter(adapter);
		        listViews.get(i).setOnItemClickListener(this);
		        textViews.get(i).setText(className);
		        cateMap.put(className, classid);
		        i++;
			}
		}
    	
        if (mHeadlineGallery == null) {
            mHeadlineGallery = (HeadlineGallery) findViewById(R.id.headline_gl);
            mHeadlineGallery.setOnItemClickListener(this);
        }
        HeadlineAdapter headlineAdapter = new HeadlineAdapter(this, HomeActivity.headLine);
        mHeadlineGallery.setAdapter(headlineAdapter);
    }


    private int getClassIndex(Map<String, String> mp,String id){
    	int i = 0;
    	for(;i<mp.size();i++){
    		if(mp.get(i).equals(id)){
    			break;
    		}
    	}
    	
    	return i;
    }
    
    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
    	
    	if(position==adapter.getAdapter().getCount()-1){
    		Map<String, Object> item = (Map<String, Object>) adapter.getItemAtPosition(position-1);
    		HomeListAdapter ad = (HomeListAdapter)adapter.getAdapter();
    		String classid = ad.getType();
    		String datetime = ModelUtils.getStringValue(item, "dateline");
    		mapcookie = new HashMap<String, String>();
    		mapcookie.put("classid", classid);
    		mapcookie.put("province", "");
    		mapcookie.put("city", "");
    		mapcookie.put("time", "3");
    		mapcookie.put("line", datetime);
    		execute(mapcookie);
			return;
    	}else{}
    	
    	Map<String, Object> item = (Map<String, Object>) adapter.getItemAtPosition(position);
    	if (item != null) {
			Intent intent = new Intent(ActivitysActivity.this, ActivitysDetailActivity.class);
			intent.putExtra(ActivityDetailInfoModel.PARAM_EVENTID, 
					ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_EVENTID));
			startActivity(intent);
		}
    }
    
	
	private void execute(Map<String, String> params) {
		cancelGetMore();
		mActivitySelectTask = new ActivitySelectTask(this,
				DataAsyncTask.TASK_ACTIVITY_GETMOREACTIVITY, this);
		Map<String, String> cookie = new HashMap<String, String>();
		cookie.put(Constant.LOGIN_COOKIE1, PrefHelper.getString(
				ActivitysActivity.this, Constant.LOGIN_COOKIE1, ""));
		cookie.put(Constant.LOGIN_COOKIE2, PrefHelper.getString(
				ActivitysActivity.this, Constant.LOGIN_COOKIE2, ""));
		cookie.putAll(params);
		mActivitySelectTask.execute(cookie);
	}
    
	private void cancelGetMore() {
		if (mActivitySelectTask != null) {
			mActivitySelectTask.cancel(true, true);
		}
		mActivitySelectTask = null;
	}
	

    @Override
    public boolean onCreateOptionsNeteaseMenu(Menu menu) {
//    	((AudienceMainActivity)getParent()).onCreateOptionsNeteaseMenu(menu);
//        menu.add(0, MENU_REFRESH, 0, R.string.menu_refresh).setIcon(R.drawable.ic_menu_refresh);
        
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (!((AudienceMainActivity) getParent()).onMenuItemSelected(featureId, item)) {
            switch (item.getItemId()) {
                case MENU_REFRESH:
                    execute();
                    break;
            }
        }
        return true;
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_REFRESH: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage(getText(R.string.refresh_button));
                dialog.setOnCancelListener(new OnCancelListener() {                    
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        if (mActivitysTask != null) {
                        	mActivitysTask = null;
                        }                        
                    }
                });
                return dialog;
            }
        }
        return super.onCreateDialog(id);
    }
    
    public void refresh() {
        if (mActivitysTask != null) {
            cancel();
        }
        showDialog(DIALOG_REFRESH);        
    }
    
    @Override
    public void onPostExecute(int what, Object o) {
        super.onPostExecute(what, o);
        switch (what) {
            case DataAsyncTask.TASK_ACTIVITYS_INFO:
            	List<Map<String, Object>> objMap = (List<Map<String, Object>>) o;
                initData(objMap);
//                dismissDialog(DIALOG_REFRESH);
                break;
            case DataAsyncTask.TASK_ACTIVITY_GETMOREACTIVITY:
            	Map<String, Object> objMap2 = (Map<String, Object>) o;
            	if(null==o || o.equals("")){
            		return;
            	}
            	List<Map<String, Object>> list = ModelUtils.getListMapValue(objMap2,
        				ActivitySelectInfoModel.PARAM_EVENTS);
        		if(list.size()==0){
        			Tips("没有数据");

        			return;
        		}else{
        			String currentclassid = (String) objMap2.get("currentclassid");
        			allDataListMap.get(currentclassid).addAll(list);
        			allListAdapterMap.get(currentclassid).notifyDataSetChanged();
        		}
            	break;
        }
    }

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(ActivitysActivity.this, ActivitySelectActivity.class);
		String name = null;
		String id = null;
		switch (v.getId()) {
		case R.id.one:
			name = textViews.get(0).getText().toString();
			break;
		case R.id.two:
			name = textViews.get(1).getText().toString();
			break;
		case R.id.three:
			name = textViews.get(2).getText().toString();
			break;
		case R.id.four:
			name = textViews.get(3).getText().toString();
			break;
		case R.id.five:
			name = textViews.get(4).getText().toString();
			break;
		case R.id.six:
			name = textViews.get(5).getText().toString();
			intent.putExtra(ACTIVITY_CATEGORY_INDEX_Y, "9");
			break;

		default:
			break;
		}
		intent.putExtra(ACTIVITY_CATEGORY, name);
		intent.putExtra(ACTIVITY_CATEGORY_INDEX, cateMap.get(name));
		startActivity(intent);
	}
}